<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\RequestException;
use App\Forms\AdminForms;
use App\Forms\RbacForms;

class AuthController extends Controller
{

    /**
     * 管理员登录
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View|mixed
     * @throws \App\Exceptions\RequestException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function login()
    {
        if(\Auth::guard('admin')->check()){
            return redirect(route('admin.home'));
        }

        if($this->request->isMethod('post')){
            $rules = [
                'name'     => 'required|string',
                'password' => 'required',
                'remember' => 'nullable',
            ];

            $this->validateInput($rules);

            return $this->successOrFailed((new AdminForms())->login($this->validated));
        }

        return $this->view('auth.login');

    }


    /**
     * 退出登录
     */
    public function logout()
    {
        \Auth::guard('admin')->logout();
        return redirect(url('auth/login'));

    }


    /**
     * 管理员列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \App\Exceptions\RequestException
     */
    public function managerList()
    {
        if($this->request->ajax()) {
            $rules = [
                'page'       => 'required',
                'limit'      => 'required',
            ];

            $this->validateInput($rules);

            return $this->showJsonLayui((new AdminForms())->getManagerList($this->validated));
        }

        return $this->view('auth.manager_list');
    }


    /**
     * 权限组列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \App\Exceptions\RequestException
     */
    public function groupList()
    {
        if($this->request->ajax()) {
            $rules = [
                'page'       => 'required',
                'limit'      => 'required',
            ];

            $this->validateInput($rules);

            return $this->showJsonLayui((new AdminForms())->getGroupList($this->validated));
        }

        return $this->view('auth.group_list');
    }


    /**
     * 修改密码
     * @return mixed
     * @throws RequestException
     */
    public function editPassword()
    {

        if($this->request->isMethod('post')) {

            $rules = [
                'password'     => 'required',
                'new_password' => 'required',
            ];

            $this->validateInput($rules);

            return $this->successOrFailed(
                (new AdminForms())->editPassword($this->validated)
            );

        }

        return $this->view('auth.edit_password');
    }


    /**
     * 删除管理员
     * @return mixed
     * @throws RequestException
     */
    public function deleteManager()
    {
        $rules = [
            'id' => 'required|not_in:1',
        ];

        $this->validateInput($rules);

        return $this->successOrFailed(
            (new AdminForms())->deleteManager($this->validated)
        );
    }

    /**
     * 修改管理员
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws RequestException
     */
    public function editManager()
    {
        $rules = [
            'id'       => 'nullable',
            'name'     => 'nullable',
            'password' => 'nullable',
            'gid'      => 'nullable',
            'phone'    => 'nullable',
        ];

        $this->validateInput($rules);

        $form = new AdminForms();
        if($this->request->isMethod('post')) {

            return $this->successOrFailed(
                $form->editManager($this->validated)
            );

        }

        return $this->view('auth.edit_manager',
            [
                'manager'    => $form->getManagerOrNew($this->validated),
                'groupList'  => $form->getGroupListNoPage()
            ]
        );
    }


    /**
     * 修改管理员分组
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws RequestException
     */
    public function editGroup()
    {
        $rules = [
            'id'      => 'nullable',
            'name'    => 'nullable',
            'menu_id' => 'nullable'
        ];

        $this->validateInput($rules);

        if($this->request->isMethod('post')) {

            $form = new RbacForms();

            return $this->successOrFailed(
                $form->editManagerGroup($this->validated)
            );

        }

        return $this->view('auth.edit_group', [
            'group' => (new AdminForms())->getGroupOrNew($this->validated)
        ]);
    }


    /**
     * 获取权限菜单
     * @return mixed
     * @throws RequestException
     */
    public function getGroupTree()
    {
        $rules = [
            'id' => 'nullable'
        ];

        $this->validateInput($rules);

        return $this->success((new RbacForms())->getManagerGroup($this->validated));
    }


    /***
     * 删除权限组
     * @return mixed
     * @throws RequestException
     */
    public function deleteGroup()
    {
        $rules = [
            'id' => 'required',
        ];

        $this->validateInput($rules);

        return $this->successOrFailed(
            (new AdminForms())->deleteGroup($this->validated)
        );
    }
}